require('./index.css')
require('page/common/top-head-menu/index.js')
require('page/common/footer/index.js')
require('page/common/head-search/index.js')
require('page/common/bread-menu/index.js')
require('page/common/right-easy-menu/index.js');

import validate from 'jquery-validation';
import _mm from 'util/mm.js';
import _tool from 'util/tool.js';
import _all from 'service/all-service';

const page ={
    init() {
        this.initValidate();
        this.bindEvent();
    },
    submitInfo() {
        var flag = $('#feedback').valid();
        if (!flag) {
            return;
        }
        const _this = this;
        let formData = $('#feedback').serializeArray();
        formData = _mm.serializeJsonObj(formData);
        console.log(formData);
        _all.sendFeedback(JSON.stringify(formData), res => {
            _tool.msgShakeLayer('Successful submission！');
        }, err => {
            _tool.msgShakeLayer(err.message);
        });
    },
    bindEvent() {
        const _this = this;
        const sendInfo = function () {
            _this.submitInfo();
        };
        $('#sendFeed').on('click', sendInfo);
    },
    initValidate() {
        jQuery.validator.addMethod("isphoneNum", function (value, element) {
            var length = value.length;
            var mobile = /^1[3|5|8]{1}[0-9]{9}$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");
        $('#feedback').validate({
            errorPlacement: function(error, element) {
                $(element).siblings('.error-tip').html(error);
            },
            ignore: [],
            rules: {
                name: "required",
                company: "required",
                country: "required",
                email: {
                    required: true,
                    email: true
                },
                phone: {
                    required: true,
                    isphoneNum: true
                }
            },
            messages: {
                name: "please enter your name.",
                company: "please enter your company name.",
                country: "please choose your country.",
                email: {
                    required: 'please enter your email.',
                    email: 'please check your email.'
                },
                phone: {
                    required: 'please enter your phone number.',
                    isphoneNum: 'please check your phone number.'
                }
            }
        });
    }
};

module.export = page.init();